<?php

include(dirname(__FILE__) . '/asimple_mysql-2009-04-12/asimple.php');

class ArenaParkDatabase extends ASimpleMySQLDB {

	var $database = null;

	public function ArenaParkDatabase() {

		parent:: __construct(SIMPLE_DB_SERVER, SIMPLE_DB_NAME, SIMPLE_DB_USERNAME, SIMPLE_DB_PASSWORD);
	}

	public function setConductor($conductor, $userid = false) {

		$conductor = $this->normalizeQuery($conductor);
		if ($userid)
			$conductor['userid'] = $userid;

		return $this->insert_array('conductor', $conductor, false);
	}

	public function setCar($car, $userid = false) {

		$car = $this->normalizeQuery($car);
		if ($userid)
			$car['userid'] = $userid;

		return $this->insert_array('car', $car, false);
	}

	public function setEnterprise($enterprise) {

		$enterprise = $this->normalizeQuery($enterprise);

		return $this->insert_array('enterprises', $enterprise, false);
	}

	public function setReservation($user, $reservation, &$car, &$conductor = false) {

		$reservation = $this->normalizeQuery($reservation); //by reference

		$reservation['date'] = date('YmdHis');

		$reservation['userid'] = $user['userid'];
		$reservation['day_date_car_in'] = $this->getDateAsNumber($reservation['day_date_car_in']);
		$reservation['date_fly'] = $this->getDateAsNumber($reservation['date_fly']);
		$reservation['date_fly_back'] = $this->getDateAsNumber($reservation['date_fly_back']);
		$reservation['day_date_car_out'] = $this->getDateAsNumber($reservation['day_date_car_out']);

		if (is_array($car)) {

			$car = $this->normalizeQuery($car);
			$car['userid'] = $user['userid'];

			if (!$this->insert_array('car', $car, false)) {
				echo 0;
				exit;
			}

			$car = $this->get_record_by_ID("car", "idcar", $this->get_last_id("car"));

			$reservation['idcar'] = $car['idcar'];
		} else {

			$reservation['idcar'] = $car;

			$car = $this->get_record_by_ID('car', 'idcar', $car);
		}


		$conductor = $this->get_record_by_ID('conductor', 'conductorid', $conductor);

		$reservation['conductorid'] = $conductor['conductorid'];

		$services = array();

		if (isset($reservation['washing'])) {

			$services[] = $reservation['washing'];
			unset($reservation['washing']);
		}
		if (isset($reservation['aspirate'])) {

			$services[] = $reservation['aspirate'];
			unset($reservation['aspirate']);
		}
		if (isset($reservation['washingAspirate'])) {

			$services[] = $reservation['washingAspirate'];
			unset($reservation['washingAspirate']);
		}

		if (!$this->insert_array('reservation', $reservation, false))
			return false;

		$result = $this->get_record_by_ID('reservation', 'reservation_number', $this->get_last_id("reservation"));

		foreach ($services as $key => $value) {

			$this->insert_array('reservation_service', array("reservation_number" => $result['reservation_number'],
				"serviceid" => $value), false);
		}
		return $result;
	}

	public function getReservationsByDate($date) {

		$date = $this->getDateAsNumber($date, true);

		$sql = "SELECT *FROM reservation WHERE date <= '$date'";

		return $this->exec_query($sql);
	}

	public function getDateAsNumber($dateString, $hour = true) {

		$year = substr($dateString, 6, 4);
		$months = substr($dateString, 3, 2);
		$days = substr($dateString, 0, 2);
		if ($hour)
			$hourMinut = date("His");
		else
			$hourMinut = '000000';

		return $year . $months . $days . $hourMinut;
	}

	public function getDateAsString($dateNumber) {

		$year = substr($dateNumber, 0, 4);
		$months = substr($dateNumber, 4, 2);
		$days = substr($dateNumber, 6, 2);

		return $days . "/" . $months . "/" . $year;
	}

	public function loguin($credentials) {

		$credentials = $this->normalizeQuery($credentials);


		$sql = "SELECT *FROM user WHERE email='" . $credentials['email'] . "' AND phone_movil=" . $credentials['phone_movil'] . ";";


		return $this->exec_query($sql);
	}

	public function loguinEnterprise($credentials) {

		$credentials = $this->normalizeQuery($credentials);

		$sql = "SELECT *FROM enterprises WHERE code='" . $credentials['enterprise_code'] . "';";

		return $this->exec_query($sql);
	}

	public function getUserFromEnterprise($enterprise) {
		$usersreturn = array();
		$enterprisereservation = "SELECT *FROM user WHERE user.member='" . $enterprise . "'";

		if ($enterprisereservation = $this->exec_query($enterprisereservation))
			return $enterprisereservation;
		return false;
	}

	public function getCarFromEnterprise($enterprise) {
		$carreturn = array();
		$sql = "SELECT car_enterprises.carid FROM car_enterprises WHERE car_enterprises.enterprisesid='" . $enterprise . "'";
		if ($carsid = $this->exec_query($sql))
			foreach ($carsid as $key => $value) {
				$sql = "SELECT *FROM car WHERE car.idcar='" . $value['carid'] . "'";
				if ($car = $this->exec_query($sql))
					foreach ($car as $key => $value2) {
						$carreturn[] = $value2;
					}
			}
		return (count($carreturn)) ? $carreturn : false;
	}

	public function getConductorFromEnterprise($enterprise) {

		$conductorreturn = array();
		$sql = "SELECT conductor_enterprises.conductorid FROM conductor_enterprises WHERE conductor_enterprises.enterprisesid='" . $enterprise . "'";
		if ($conductorid = $this->exec_query($sql))
			foreach ($conductorid as $key => $value) {
				$sql = "SELECT *FROM conductor WHERE conductor.conductorid='" . $value['conductorid'] . "'";
				if ($conductor = $this->exec_query($sql))
					foreach ($conductor as $key => $value2) {
						$conductorreturn[] = $value2;
					}
			}
		return (count($conductorreturn)) ? $conductorreturn : false;
	}

	public function findUser($text, $field){
		
		$usersquery = "SELECT *FROM user WHERE user.$field='" . $text . "'";

		return ($usersquery = $this->exec_query($usersquery)) ? $usersquery : false;
	}
	
	public function deleteUser($userid){
		
		$usersquery = "DELETE FROM user WHERE user.userid='$userid';";

		return ($usersquery = $this->exec_query($usersquery)) ? $usersquery : false;
	}
	
	public function getUserById($userid) {

		$usersquery = "SELECT *FROM user WHERE user.userid='" . $userid . "'";

		return ($usersquery = $this->exec_query($usersquery)) ? $usersquery : false;
	}

	public function getUserPaginator($start = 0, $end = 20) {

		$range = $_SESSION['paginar'];
		if ($range)
			$usersquery = "SELECT * FROM user ORDER BY userid DESC LIMIT $start, $range";
		else
			$usersquery = "SELECT * FROM user ORDER BY userid DESC";
		return $this->exec_query($usersquery);
//		echo count($usersquery);
//		exit;
	}

	public function updateUser($user) {
// 	echo var_dump($user);exit;
		$dataToUpdate = "";
		$userid;
		foreach ($user as $key => $value) {
			if ($value[0] !== 'userid') {
				if ($dataToUpdate)
					$dataToUpdate .= ", $value[0]='$value[1]'";
				else
					$dataToUpdate .= "$value[0]='$value[1]'";
			}
			else
				$userid = $value[1];
		}
		$usersquery = "UPDATE user SET $dataToUpdate WHERE userid=$userid";

		return ($this->exec_query_update($usersquery)) ? $userid : false;
	}

	public function updateConductor($conductor) {
// 	echo var_dump($user);exit;
		$dataToUpdate = "";
		$conductorid = false;
		foreach ($conductor as $key => $value) {
			if ($value[0] !== 'conductorid') {
				if ($dataToUpdate)
					$dataToUpdate .= ", $value[0]='$value[1]'";
				else
					$dataToUpdate .= "$value[0]='$value[1]'";
			}
			else
				$conductorid = $value[1];
		}
		$conductorsquery = "UPDATE conductor SET $dataToUpdate WHERE conductorid=$conductorid";

		return ($this->exec_query_update($conductorsquery)) ? $conductorid : false;
	}

	public function updateCar($car) {
// 	echo var_dump($user);exit;
		$dataToUpdate = "";
		$carid = false;
		foreach ($car as $key => $value) {
			if ($value[0] !== 'idcar') {
				if ($dataToUpdate)
					$dataToUpdate .= ", $value[0]='$value[1]'";
				else
					$dataToUpdate .= "$value[0]='$value[1]'";
			}
			else
				$carid = $value[1];
		}
		$carsquery = "UPDATE car SET $dataToUpdate WHERE idcar=$carid";

		return ($this->exec_query_update($carsquery)) ? $carid : false;
	}

	public function countRecords($table) {

		$return = "SELECT COUNT(*) FROM $table";

		return ($return = $this->exec_query($return)) ? $return[0]['COUNT(*)'] : false;
	}

}

?>
